﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using University.Workshop.Model;
using University.Workshop.DataAccess;
using System.Globalization;
using System.Text.RegularExpressions;

namespace University.Workshop.Business
{

    public static class SearchProductsManager
    {
        public static Collection<Product> SearchedProducts(String sentence, String category)
        {
            String[] words = null;
            String format = @"^[a-zA-Z0-9 ]";
            if (!String.IsNullOrEmpty(sentence))
            {
                if (!Regex.IsMatch(sentence, format))
                {
                    throw new ValidationException("Incorrect format input text");
                }
                words = SeparateWords(sentence);
            }
            Collection<Product> allProducts = new Collection<Product>();
            Collection<Product> resp = new Collection<Product>();
            allProducts = ProductsByCategory(category);
            if (allProducts != null && allProducts.Count != 0)
            {
                if (words != null)
                {
                    allProducts = GetSearchProducts(words, allProducts);
                    resp = DropRepeatedValues(allProducts);
                }
                else
                {
                    resp = allProducts;
                }
            }
            else
            {
                throw new BusinessException("No products registered");
            }
            return resp;
        }
        public static String[] SeparateWords(String sentence)
        {
            String[] words = null;
            if (!String.IsNullOrEmpty(sentence))
            {
                words = sentence.Split(' ');
            }
            return words;
        }
        public static Collection<Product> ProductsByCategory(String category)
        {
            
            Collection<Product> products = new Collection<Product>();
            if (!String.IsNullOrEmpty(category))
            {
                if (category != "All Products")
                {
                        products = ProductDataAccess.GetProductsAuctionByCategory(category);
                }
                else
                {
                        products = (Collection<Product>)ProductDataAccess.Products();
                }
            }
            else
            {
                throw new ValidationException("No Category selected");
            }
            return products;
        }

        public static Collection<Product> DropRepeatedValues(Collection<Product> repeatedValues)
        {
            Collection<Product> noRepeatedValues = new Collection<Product>();
            int j = 0, i;
            if (repeatedValues!=null && repeatedValues.Count != 0 )
            {
                foreach (Product prod1 in repeatedValues)
                {
                    i = 0;
                    if (j == 0)
                    {
                        noRepeatedValues.Add(prod1);
                    }
                    foreach (Product prod2 in noRepeatedValues)
                    {
                        if (prod1.ProductId != prod2.ProductId)
                        {
                            if (i == noRepeatedValues.Count - 1)
                            {
                                noRepeatedValues.Add(prod1);
                                break;
                            }
                        }
                        else
                        {
                            break;
                        }
                        i++;
                    }
                    j++;
                }
            }
            return noRepeatedValues;
        }

        public static Collection<Product> GetSearchProducts(String[] words, Collection<Product> products)
        {
            Collection<Product> resp = new Collection<Product>();
            if (products!= null && products.Count!=0)
            {
                if (words!=null && words.Length != 0)
                {
                    foreach (String word in words)
                    {
                        foreach (Product prod in products)
                        {
                            String name = prod.Name.ToLower(CultureInfo.CurrentCulture);
                            String description = prod.Description.ToLower(CultureInfo.CurrentCulture);
                            String lword=word.ToLower(CultureInfo.CurrentCulture);
                            if (!String.IsNullOrEmpty(word) && (name.Contains(lword) || description.Contains(lword)))
                            {
                                resp.Add(prod);
                            }
                        }
                    }
                }
            }
            return resp;
        }
    }
}
